package com.lkwlw.tplms.transport.action;

import java.text.SimpleDateFormat;
import java.util.Map;

import com.lkwlw.tplms.order.dao.mapper.OrderinfoMapper;
import com.lkwlw.tplms.order.dao.model.Orderinfo;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AddOrderAction extends ActionSupport{
	
	private static final long serialVersionUID = -7513348008427351779L;
	
	private Orderinfo order;	
	private String sendDate;	
	private OrderinfoMapper orderInfoDAO;
	private String tipMessage;	
	private String orderId;

	@Override
	public String execute() throws Exception {
		//clearAction();
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		Admininfo user = (Admininfo) session.get("admin");
		order.setReceiver(user.getFullName());
		return SUCCESS;		
	}
	
	public String query() throws Exception{
		order = orderInfoDAO.selectByPrimaryKey(orderId);
		if (null != order && "未收件".equals(order.getState())) {
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			Admininfo user = (Admininfo) session.get("admin");			
			if (null != order.getSenddate()) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				sendDate=format.format(order.getSenddate());
			}			
			order.setReceiver(user.getFullName());
		}else if (null == order) {
			tipMessage = "norecord";
		}
		orderId = "";
		return SUCCESS;
	}
	private void clearAction() {
		
	}

	public String save() throws Exception {
		orderId = order.getId();
		if (null != orderId && !"".equals(orderId)) {
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			Admininfo user = (Admininfo) session.get("admin");
			String state = order.getState();
			if (null != state && !"".equals(state)) {
				state = state.equals("未收件") ? "已收件" : state;
			}
			order.setState(state);
			order.setRegistrant(user.getFullName());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			order.setSenddate(format.parse(sendDate));
			orderInfoDAO.updateByPrimaryKeySelective(order);
			tipMessage = "success";
			order = orderInfoDAO.selectByPrimaryKey(orderId);
		}		
		orderId = "";
		return SUCCESS;
	}
	
	public Orderinfo getOrder() {
		return order;
	}

	public void setOrder(Orderinfo order) {
		this.order = order;
	}

	public String getSendDate() {
		return sendDate;
	}

	public void setSendDate(String sendDate) {
		this.sendDate = sendDate;
	}

	public void setOrderInfoDAO(OrderinfoMapper orderInfoDAO) {
		this.orderInfoDAO = orderInfoDAO;
	}
	

	public String getTipMessage() {
		return tipMessage;
	}

	public void setTipMessage(String tipMessage) {
		this.tipMessage = tipMessage;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

}
